Skip to content

fix(app-logic): bound memory for remote file reads - #1728

Merged
smart-ssh-coordinator[bot] merged 4 commits into
release/0.8.0from
issue-1714-bound-memory-for-remote-file-reads-in
Dec 4, 2026
Merged

smart-ssh-coordinator[bot] merged 4 commits into
release/0.8.0from
issue-1714-bound-memory-for-remote-file-reads-in

Conversation

@smart-ssh-coder

@smart-ssh-coder smart-ssh-coder Bot commented Dec 4, 2026 •

Copy link
Copy Markdown
Contributor

Closes #1714

Summary

  • Text preview and open-for-editing read with read_file_capped(limit + 1); the existing "too large" errors/codes are returned for a longer result (also when stat fails or lied).
  • Backup before a confirmed write copies in 256 KiB chunks (read_range + write_range), hashing incrementally. The copy is bounded by the stat size (a file yielding more bytes fails the write); any read/write error fails the write before the target is touched and removes the partial backup best-effort.
  • Sudo-fallback "before" hash uses read_file_chunked, bounded by the stat size; a read error or a larger file keeps sha256_before = None (remote_files.rs:hash_tests::test_hash_old_content_is_bounded_by_the_stat_size).
  • Extra tests: tests_activity.rs:test_backup_of_a_file_larger_than_its_stat_size_fails_and_leaves_no_backup, test_backup_failing_on_a_later_chunk_removes_the_partial_backup (counter-proof: with the removal disabled it fails), size_limit_tests.rs:test_t5_a_grown_file_creates_no_edit_directory.
  • Guard test: no .read_file( call in production code of app-logic / app-shell (only the elevated wrapper forwards it).

Acceptance Criteria

  • Preview with failing stat: met — size_limit_tests.rs:test_t2_a_failing_stat_no_longer_lets_an_oversized_file_through (asserts only a capped read with limit MAX_TEXT_PREVIEW_BYTES + 1).
  • Open for editing, stat small / content large: met — size_limit_tests.rs:test_t5_a_file_that_grew_between_stat_and_read_leaves_no_local_copy (limit + 1, existing local copy untouched, no dir created).
  • Backup byte-for-byte + sha256_before, no unbounded read: met — tests_activity.rs:test_backup_of_a_multi_chunk_file_is_exact_and_read_in_bounded_pieces (plus empty file and read-error tests).
  • Sudo fallback chunked hash / read error → None: met — tests_activity.rs:test_sudo_fallback_hashes_the_old_file_from_a_chunked_read, test_sudo_fallback_read_error_leaves_the_hash_none_and_still_writes.
  • Guard: met — crates/app-logic/tests/no_unbounded_read_file.rs.
  • Existing limit/revocation/write tests: pass; T1/T2/T5 expectations only changed from read_file to the capped read.

Tests

cargo test -p app-logic, -p app-shell, -p ssh-manager-core, clippy all green locally. No counter-proof run (the old code path called unbounded read_file, which the updated mock expectations and the guard test would flag).

Assumptions

  • read_file stays in the trait: it is still used by transport tests and the local/elevated wrappers; removing it would touch many mocks.
  • The mock gained with_read_failure (permission-style error) to test the read-error cases.

Spec: updated 0086 (§2 non-goal, A1.2, A1.6, T5) and 0020 (§4.2 point 4: bounded backup copy).
Threat model: no change – only how existing file content is read changes, no new boundary.

Manual test steps

None needed.

Follow-ups

None.

🤖 Generated with Claude Code

smart-ssh-coder Bot and others added 2 commits December 4, 2026 17:33
…pen and write backup (#1714)

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>

@smart-ssh-reviewer smart-ssh-reviewer Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review

Verdict: changes requested
Review level: elevated

Blocking

  • docs/specs/0086-sftp-limits-and-session-pairing.md:24-27 (also §1 l.13, A1.2, A1.6, T3c/T5): the spec update is missing, and the spec now contradicts the code. The issue sets Spec impact: update 0086, but the PR body says Spec: none. §2 still lists as a non-goal "Kein begrenztes Lesen für „Lokal öffnen“ und die Vorschau des Upload-Vergleichs: Eine Datei darf dort kurz ganz im Speicher liegen". After this PR both read at most limit + 1 byte (copy_content.rs:81-83, edit_session.rs:147-150). Fix:
    • Remove or rewrite that non-goal.
    • A1.2: "Open locally" reads at most 50 MB + 1 byte, also when the file grew after the size query.
    • A1.6: the upload-comparison preview reads at most 256 KB + 1 byte, also when the size query fails, with the same "too large" message.
    • Adjust §1 ("sonst nach dem Lesen") accordingly.
    • Add test cases for a failed or understated size query (T3c twin; T5 reads only limit + 1).
    • Also add one sentence to Spec 0020 §4.2 point 4 on what happens when the backup copy fails or the old file does not end (see the next findings).
  • crates/app-logic/src/orchestration/remote_files.rs:352-377 (copy_to_backup) and :471-482 (sudo-fallback hash): the copy and the hash have no end condition other than EOF. The issue's goal names "a huge or growing file, a special file, or a server whose stat answer does not match the content". Memory is now bounded, but a target that never ends still makes the action run forever. Examples: a confirmed WriteRemoteFile on a path that is a symlink to /dev/zero, a FIFO or device that stat finds (existed = true), a log that grows faster than it is copied, or a server that never reports EOF. Each turns "client out of memory" into "backup file grows until the server disk is full". The action hangs and holds the session's SFTP lock, so the file browser blocks too. This also applies to the local pseudo-server, where it fills the user's own disk. The sudo fallback's read_file_chunked(..., Continue) hangs the same way. The caller already knows the stat size (bytes_before, remote_files.rs:601). Fix:
    • Pass bytes_before into copy_to_backup and fail the write with an error once the copy reads past it. The target stays untouched.
    • In the sudo fallback, count the bytes in the closure and return ChunkControl::Stop past that size, so sha256_before stays None for Stopped.
    • Add a test with a mock that keeps delivering data past the reported size.
  • crates/app-logic/src/orchestration/remote_files.rs:362-372: a failed copy leaves a truncated backup under a name that says "backup". If a read or write fails on a later chunk (I/O error, disk full, a server failing the second read_range), ? returns after earlier write_range calls already went through. A partial <path>.smartssh-backup-<ts> stays on the server. On the base, a read error happened before any backup write, so no backup file existed. A user restoring from it gets a truncated file. The target is still untouched, which is correct. Fix:
    • On any error after the first write_range, remove the backup as best effort (also on the abort from the previous finding). Alternatively, write to a temporary name and rename it once the copy is complete.
    • Add a test whose read fails on the second chunk. The existing test_backup_read_error_leaves_the_target_untouched only fails at offset 0.
  • crates/app-shell/src/commands/sftp/size_limit_tests.rs:501-535 (test_t5_…): AC 2 ("creates no file or directory in the edit root") is not proven for the grown-file case. The test calls create_dir_all(&dir) before the call, so it cannot show that no directory is created. It also does not assert that no new file appeared there. It only checks that the earlier copy is unchanged. T4 checks !dir.exists(), but only when stat already reports too large. The production order is correct (the check runs before edit_session_dir_in), but the criterion asks for the test. Fix: add a variant without the pre-created directory that asserts !dir.exists() when stat reports a small size and the content is over max_bytes. Or assert that the directory holds only the earlier copy.
  • crates/app-shell/src/commands/sftp/copy_content.rs:38-42, :70-73, :86-92, and crates/app-shell/src/commands/sftp/edit_session.rs:156-159: the comments contradict the code. They still talk about read_file ("die eigentliche read_file-Fehlermeldung", "zwischen stat und read_file"). The comment at copy_content.rs:89 also says a failed stat leaves the file "bewusst ganz ungeprüft", but the read is now capped. Fix: name read_file_capped. Say instead that a failed stat no longer matters for memory, because at most limit + 1 byte is read, and the check after the read decides.

Non-blocking

  • PR body: Spec: none (…) → Spec: update 0086 (and 0020).
  • changelog.d/issue-1714-bounded-remote-file-reads.md:2: "Text preview" is not a UI name. Suggested: "The upload comparison preview, "Open locally" and the backup before a confirmed file write no longer load a huge or growing remote file into memory."
  • crates/app-logic/tests/no_unbounded_read_file.rs:1-4: #![allow(clippy::unwrap_used)] splits the module doc in two. The text search for .read_file( misses a call written as SftpSession::read_file(sftp, p). Adding ::read_file( to the search would close that gap. Acceptable for a guard test.
  • remote_files.rs:360-375: the loop checks chunk.is_empty() twice. "Write the first chunk, then break on empty" would read more easily. This is optional on a release branch.
  • remote_files.rs:360: read_range reopens the file for every chunk. If the file is replaced atomically (rename) during the copy, the backup mixes old and new content. The old read_file read from one handle. This is rare (see Follow-ups).
  • The empty commit 18167853 (CI re-run) can be dropped at squash-merge.

Verified

  • counter-proof: production files reset to origin/release/0.8.0 → red: test_backup_of_a_multi_chunk_file…, test_backup_read_error…, test_sudo_fallback_hashes…, test_sudo_fallback_read_error…, size_limit_tests::test_t1/t2/t5, no_unbounded_read_file (reports exactly the four call sites the issue names); green again with the fix (executed). test_backup_of_an_empty_file… is green on the base too, as expected.
  • AC1 → met (size_limit_tests.rs:test_t2_a_failing_stat_…, ops [stat, read_file_capped limit=MAX_TEXT_PREVIEW_BYTES+1]) (executed)
  • AC2 → partly met: error code and limit+1 read are checked; "no directory" is not covered for this case (see Blocking) (read + executed)
  • AC3 → met (2×256 KiB + 123 bytes, byte-exact backup, sha256_before, no read_file) (executed)
  • AC4 → met (chunked hash; read error → None, write proceeds) (executed)
  • AC5 → met; the only .read_file( left outside tests in all crates is the elevated wrapper elevation/sftp_impl.rs:24 (executed + read)
  • AC6/AC7 → met; revocation_tests.rs unchanged and green.
  • edge cases: empty file → ok (empty backup) | exactly at the limit → ok (T3/T6) | chunk boundaries → ok | max_bytes = u64::MAX → saturating_add, ok | revocation between stat and read on the elevated channel → ok (wrapper checks each op, T6b) | read error at a later chunk → partial backup (Blocking) | endless/growing/special file → endless copy (Blocking)
  • callers checked: read_text_impl, open_for_editing_impl, write_via_sftp_with_backup, write_via_sudo_fallback (one caller each) → ok. A SftpPermissionDenied from read_range still leads to the sudo fallback. (read)
  • tests run locally: cargo test --locked -q -p app-logic → 1425 + 37 passed; -p app-shell → 516 + 4 passed; -p ssh-manager-core --lib ssh:: → 80 passed (executed)
  • UI: not a UI change
  • data: not touched

Timing analysis

  • 3 test files analysed (5 new tests in tests_activity.rs, T1/T2/T5 in size_limit_tests.rs, the guard test): no timing dependency found (mock-only, no sleeps/clocks/ports; path comparison is separator-safe).

Coherence

  • comments: 4 places checked → copy_content.rs:38-42, 70-73, 86-92, edit_session.rs:156-159 blocking (see above); all other comments match.
  • fit: the change fits (256 KiB matches TRANSFER_CHUNK_BYTES, read_range/write_range is the right pair for a copy, the limit + 1 pattern matches remote_files.rs); loop readability is optional.

Spec and boundary

  • visible behaviour changed: the preview and "Open locally" now read with a bound; the backup is copied in chunks (a partial backup can remain on failure); the sudo hash is chunked; messages, codes and limits are unchanged.
  • spec searched (smartssh-backup, Lokal öffnen, Upload-Vergleich, Speicher), spec covering it: 0086 §1, §2, A1.2, A1.3, A1.6, T3c, T5; 0020 §4.2 point 4, §4.3.
  • verdict: missing (blocking): update 0086, plus one sentence in 0020 §4.2.4.
  • boundary, CI changes, vendor: ok; no .github/ changes; scope stays within the issue on the release branch; threat-model note "no change" holds.

Security checks

  • Checked: filter/confirmation path unchanged, per-operation revocation (Spec 0085 A1.1) incl. each read_range/write_range chunk, no content reaches the AI context/logs (hash only), capped reads bounded in both transports (take), read_range never returns more than len.
  • security review (subagent): 8 bypass attempts; preview and edit-open blocked in all of them →
    • endless backup copy / sudo hash on a never-ending target: blocking (confirmed by reading the code; no regular-file or size check in the caller)
    • partial backup after a mid-copy error: blocking (confirmed)
    • guard test only matches .read_file(: non-blocking, unsure (no such call exists today)

UI review

  • not a UI change

Follow-ups

  • Backup copy via read_range reopens the file per chunk; an atomic replace during the copy yields a mixed backup. Consider reading the backup from one open handle (needs a trait method that copies with a single handle, or a size/mtime re-check after the copy). (no existing issue)

…artial backups (#1714)

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@smart-ssh-coder

Copy link
Copy Markdown
Contributor Author

Review points: 1 fixed in 3d4b57e (Spec 0086 and 0020 updated). 2 fixed in 3d4b57e (backup copy and sudo hash bounded by stat size, tests added). 3 fixed in 3d4b57e (partial backup removed best-effort, test on second chunk). 4 fixed in 3d4b57e (test_t5_a_grown_file_creates_no_edit_directory). 5 fixed in 3d4b57e (comments updated).

@smart-ssh-reviewer smart-ssh-reviewer Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review

Verdict: changes requested
Review level: elevated

Follow-up round: delta 18167853..3d4b57ed. Most of the earlier points are fixed. Four small items remain. Two are in the spec text, one is a gap in the new cleanup, and one is a comment.

Blocking

  • docs/specs/0020-sftp-file-transfer.md:114-117 together with §4.3 point 4: the new sentence promises a size limit for every backup, but the sudo backup does not have one. §4.3 point 4 says the backup "aus 4.2, Punkt 4" is also made with sudo -S cp -p in the sudo case. On that path remote_files.rs:538-544 runs cp on the server. Nothing limits that copy by the size checked before the write, and nothing removes a partial copy. Only the "before" hash is limited. Fix:
    • Limit the 4.2.4 sentence to the regular write, e.g. "Beim regulären Schreiben ist die Kopie …".
    • Add a half-sentence to §4.3 point 4: the sudo copy is made on the server and is not limited by that size; only the stored SHA-256 "vorher" is, and it is missing when the file returns more bytes.
  • crates/app-logic/src/orchestration/remote_files.rs:398-403 (copy_to_backup_inner): a failure on the first chunk still leaves a partial backup. created is set only after the first write_range(…, true) succeeds. The real write_range (crates/ssh-transport/src/sftp.rs:396-409) first calls create (CREATE|TRUNC) and then write_all. Scenario: the server disk is nearly full and create succeeds, but writing the first 256 KiB chunk fails with ENOSPC. The action then fails and the target stays untouched, which is correct. But a truncated <path>.smartssh-backup-<ts> stays on the server and is not removed. That contradicts the new Spec 0020 sentence ("eine angefangene Sicherungskopie wird entfernt") and the earlier request ("remove the backup on any error"). The same happens with the write_range(backup, 0, &[], true) for an empty file. Fix:
    • Set *created = true before each first write_range(…, true) call, not after it. The name carries a timestamp, so removing it after a failed create only touches a file this action would have truncated anyway.
    • Add a test where the first backup write_range fails. For example, the mock could get a write-failure switch for a path prefix.
  • crates/app-logic/src/orchestration/remote_files.rs:527-528: the comment contradicts the code. It says the "before" hash exists whenever the old content is readable, and is None only when the read is denied or fails. Since this change, hash_old_content also returns None when the file returns more bytes than stat reported. Fixed comment: // "Before" hash: only when the old content is readable over SFTP (e.g. a 0644 root file) and no larger than its stat size; None otherwise.
  • docs/specs/0086-sftp-limits-and-session-pairing.md:13-15 (§1) and :165 (T3c): these were requested last round and are still open.
    • §1 still says „Lokal öffnen“ rejects "vor dem Lesen, wenn die Größe schon bekannt ist, sonst nach dem Lesen". A1.2, however, says a failed size query aborts the command, and the read stops at 50 MB + 1 byte. Suggested: "… vor dem Lesen nach der gemeldeten Größe, spätestens nach 50 MB + 1 gelesenem Byte, falls die Datei größer ist als gemeldet."
    • T3c needs the twin for a failed or too-small size query in the upload-comparison preview: "lehnt über 256 KB ab, auch wenn stat scheitert oder zu wenig meldet; gelesen werden höchstens 256 KB + 1 Byte". The code and test_t2_a_failing_stat_… already cover it.
    • Optionally, name A1.6 next to A1.1 and A1.2 in the §2 non-goal.

Non-blocking

  • changelog.d/issue-1714-bounded-remote-file-reads.md:2: "Text preview" is not a UI name (raised last round). Suggested: "The upload comparison preview, "Open locally" and the backup before a confirmed file write no longer load a huge or growing remote file into memory."
  • tests_activity.rs:test_backup_of_a_file_larger_than_its_stat_size_… uses a reported size of 0. The overflow is therefore caught before any backup exists. A variant with with_reported_size(path, 256 * 1024) would cover "partial copy, then the probe finds more bytes, then the backup is removed". The code handles that case already (read).
  • Behaviour change: a file whose stat size is 0 but which returns content (procfs/sysfs/some FUSE files) can no longer be written by the AI write action over SFTP. The backup now fails with "larger than its reported size". This is fail-closed and matches the issue goal. The new Spec 0020 sentence covers it.
  • remote_files.rs:511: the #[allow(clippy::too_many_arguments)] has no reason. Next to it, execute_write_remote_file uses #[expect(…, reason = …)].
  • PR body: once the spec fix above is in, also name Spec 0020 §4.3.

Verified

  • counter-proof (executed, then reverted, worktree clean):
    • Disabling the probe check turns test_backup_of_a_file_larger_than_its_stat_size… red.
    • Removing sftp.remove(backup) turns test_backup_failing_on_a_later_chunk… red.
    • Disabling the total > size check turns hash_tests::test_hash_old_content_is_bounded_by_the_stat_size red.
    • Creating the edit dir before the post-read check turns test_t5_a_grown_file_creates_no_edit_directory red.
    • All four are green with the fix.
  • AC2 → met: size_limit_tests.rs:test_t5_a_grown_file_creates_no_edit_directory asserts !dir.exists() (executed).
  • AC3/AC4 → met, unchanged since last round. The new stat-size bound is added on top (executed).
  • edge cases (read + executed):
    • exactly at the stat size → ok
    • file shrinks after stat → ok (backup and hash cover what is there)
    • empty file → ok (empty backup)
    • stat size 0 with content → fails closed
    • bytes_before None while existed → impossible (same stat match), unwrap_or(0) is never reached
    • probe read error after a partial copy → backup removed
    • first-chunk write failure → partial backup left (Blocking)
  • callers checked: copy_to_backup, write_via_sftp_with_backup, write_via_sudo_fallback, hash_old_content (1 call site each) → ok. A SftpPermissionDenied on a later chunk still leads to the sudo fallback. The overflow error is a ChannelError, so it does not (read).
  • tests run locally (executed):
    • cargo test --locked -q -p app-logic → 1428 + 37 passed
    • -p app-shell → 517 + 4 passed
    • -p ssh-manager-core ssh:: → 80 passed
    • clippy -D warnings, cargo fmt --all --check and check-file-size → clean
  • UI: not a UI change
  • data: not touched
  • CI: all checks green on 3d4b57ed.

Timing analysis

  • 3 test files analysed (two new backup tests, the new T5 variant, hash_tests, mock helpers): no timing dependency found (mock-only, counters under a mutex, no sleeps or clocks).

Coherence

  • comments:
    • The earlier findings in copy_content.rs and edit_session.rs are fixed.
    • The new comments on copy_to_backup, hash_old_content and the mock helpers match the code.
    • remote_files.rs:527-528 is blocking (see above).
  • fit: the double is_empty check is gone. Threading old_size next to existed (both come from the same stat) needs the clippy allow and the impossible unwrap_or(0) → follow-up (release base).

Spec and boundary

  • visible behaviour changed:
    • The preview and "Open locally" read at most limit + 1 byte.
    • The backup fails the write when the file returns more than its stat size, and a partial backup is removed.
    • The sudo "before" hash is missing for such a file.
  • spec searched (Lokal öffnen, Upload-Vergleich, Sicherungskopie, smartssh-backup, Vorschau), spec covering it: 0086 §1, §2, A1.2, A1.6, T3c, T5; 0020 §4.2.4, §4.3.4; 0057 (activity entry; optionally note that the SHA-256 "vorher" can be missing).
  • verdict:
    • Mostly present: the §2 non-goal, A1.2, A1.6, T5 and 0020 §4.2.4 are updated.
    • 0020 overclaims for the sudo path (blocking).
    • 0086 §1 and T3c are still open (blocking).
  • boundary, CI changes, vendor: ok. No .github/ changes, and the scope stays within the issue on the release branch. The threat-model note "no change" holds.

Security checks

  • Checked:
    • The regular write path fails closed: no target write after any backup error.
    • The bound on the backup copy and on the sudo hash.
    • The cleanup scope: remove only unlinks the timestamped name, never a symlink's target.
    • No new hang while the SFTP lock is held.
    • Per-operation revocation: not touched, because this path uses the normal channel.
  • security review (subagent): 8 bypass attempts (/dev/zero symlink, growing log, FIFO, shrinking file, mid-copy error, oversized file in the sudo path, existing file or symlink at the backup path) →
    • first-chunk create-then-fail leaves a partial backup: blocking (confirmed by reading sftp.rs:396-409)
    • the sudo fallback still runs an unbounded sudo cp -p for a root-only, never-ending file: non-blocking, unsure (pre-existing, not new in this PR) → Follow-ups
    • the guard test only matches .read_file(: non-blocking, unsure (unchanged)

UI review

  • not a UI change

Previous findings

  • Spec 0086/0020 not updated → mostly fixed in 3d4b57ed; §1 and T3c still open, and 0020 overclaims for sudo (see Blocking).
  • Endless backup copy / sudo hash → fixed in 3d4b57ed.
  • Partial backup after a mid-copy error → fixed for later chunks in 3d4b57ed; still open for the first chunk (see Blocking).
  • AC2 test without the pre-created dir → fixed in 3d4b57ed.
  • Stale comments in copy_content.rs / edit_session.rs → fixed in 3d4b57ed.

Follow-ups

  • The sudo fallback copies the old file with an unbounded sudo -S cp -p. A root-only target that never ends (for example a symlink to an endless source) can fill the server disk. Possible fixes: abort before cp when the hash stopped because the file exceeded its stat size, or use a bounded copy. (no existing issue)
  • The backup copy via read_range reopens the file for each chunk. An atomic replace during the copy yields a mixed backup. Fix: read the backup from one open handle, or re-check size and mtime afterwards. (from last round; no existing issue)
  • write_via_sftp_with_backup / write_via_sudo_fallback: pass only old_size: Option<u64> (or an OldFile { mode, size } from the stat) instead of existed plus old_size. This drops the clippy allow and the impossible unwrap_or(0).
  • The guard test no_unbounded_read_file.rs could also match ::read_file(.
  • Writing to pseudo files with a stat size of 0 through the AI write action now fails at the backup step. Consider a clearer user-facing message.

…n specs (#1714)

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@smart-ssh-coder

Copy link
Copy Markdown
Contributor Author

Review points: 1. fixed in f301cc0 (§4.2.4 limited to regular write; §4.3.4 notes the unbounded sudo copy). 2. fixed in f301cc0 (created set before the first write; test test_backup_failing_on_the_first_write_removes_the_partial_backup fails without the fix). 3. fixed in f301cc0 (comment). 4. fixed in f301cc0 (spec 0086 §1 wording, new T3d for the preview size-query case; changelog wording).

@smart-ssh-reviewer smart-ssh-reviewer Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review

Verdict: approved
Review level: elevated

Follow-up round: delta 3d4b57ed..f301cc02. All blocking points from the last round are fixed. What remains is non-blocking.

Blocking

  • None.

Non-blocking

  • docs/specs/0086-sftp-limits-and-session-pairing.md:12-14 (§1): the spec review flagged „sonst“ as blocking. I am overruling that, and here is why. The sentence reads "vor dem Lesen, wenn die Größe bekannt ist; sonst oder bei zu klein gemeldeter Größe werden höchstens 50 MB + 1 Byte gelesen". „Sonst“ refers to a case where "Open locally" never reads anything: a failed size query aborts the command (edit_session.rs:138, A1.2). "Höchstens 50 MB + 1 Byte" therefore still holds. The normative A1.2 a few lines below states the abort correctly. Still, the summary is misleading. Clearer wording: „„Lokal öffnen“ lehnt Dateien über 50 MB vor dem Lesen anhand der Größenabfrage ab; meldet sie zu wenig, wird nach höchstens 50 MB + 1 Byte abgelehnt. Scheitert die Größenabfrage, bricht der Befehl ab.“
  • crates/core/src/ssh/mock.rs:119-120: the doc says a matching write_range "creates or truncates the file … and then fails". It only does that when truncate is true; a later-chunk write just fails. Suggested: "fails (e.g. ENOSPC) after the truncating open has already created/truncated the file, as a real server does." The new Inner field write_range_fails_containing (:64) has no doc comment, unlike its neighbours.
  • Tests: no test covers a failing empty-file backup write (remote_files.rs:408-412). The code is correct, since created is set first. The oversize test still uses a reported size of 0, so "partial multi-chunk copy, then the probe finds more bytes, then the backup is removed" has no test (the code handles it). Both were left open without a stated reason.
  • remote_files.rs:401-405: created now means "a (partial) backup may exist", so backup_may_exist would be a clearer name. Optional on the release branch.
  • PR body: the Spec: line should also name 0086 §1/T3d and 0020 §4.3 point 4. The Summary still says "Text preview".
  • Spec 0086 §2 non-goal: it could name A1.6 next to A1.1/A1.2 (optional, raised before).

Verified

  • previous blocking finding (partial backup after a first-write failure) → fixed. *created = true is now set before every truncating write_range, in both the chunk loop and the empty-file branch (read: remote_files.rs:401-412).
  • mock realism: write_range(.., truncate=true) inserts the empty file before the new failure switch applies, so the new test is meaningful. The field defaults to None, so other mock users are unchanged (read: crates/core/src/ssh/mock.rs:393-411).
  • counter-proof: remote_files.rs restored from 3d4b57ed → test_backup_failing_on_the_first_write_removes_the_partial_backup red (backup path left behind). Green with the fix; worktree clean afterwards (executed).
  • edge cases (read):
    • create fails before the file exists → remove of a missing path, error ignored, original error returned → ok
    • empty file with a failing write → cleanup runs → ok (no test)
    • failure on a later chunk → ok (existing test)
    • file larger than its stat size → ok
    • SftpPermissionDenied still reaches the sudo fallback; the returned error is unchanged → ok
  • callers checked: copy_to_backup (1 call site) → ok; with_write_range_failure_containing (1 call site, the new test) → ok (read)
  • tests run locally (executed):
    • cargo test --locked -q -p app-logic → 1429 passed, plus integration and doc tests, 0 failed
    • cargo test --locked -q -p ssh-manager-core → 935 passed, plus integration tests, 0 failed
  • UI: not a UI change
  • data: not touched
  • CI: all checks green on f301cc02, including Test on all three OSes, Browser Tests, Dependency Audit and threat-model-note.

Timing analysis

  • 1 test file (plus the mock) analysed: no timing dependency found. approve_first_proposed_action waits for the proposed event, and the confirmation is registered before that event. The mock is deterministic, and the asserts run after propose (including cleanup) has finished.

Coherence

  • comments: 3 checked. remote_files.rs:401-402 and :531-533 match the code; the earlier finding at :527-528 is fixed. mock.rs:119-120 is imprecise → non-blocking. It is test-helper documentation and correct for the first-chunk case it was written for.
  • fit: the change fits. The created → backup_may_exist rename is a follow-up (release base).

Spec and boundary

  • visible behaviour changed:
    • The preview and "Open locally" read at most limit + 1 byte.
    • The regular backup is bounded by the stat size, and a partial backup is now also removed after a first-chunk failure.
    • The sudo "before" hash is missing for a file larger than its reported size.
  • spec searched (Lokal öffnen, Sicherungskopie, Backup, Upload-Vergleich, 50 MB), spec covering it: 0086 §1, §2, A1.2, A1.6, T3d, T5; 0020 §4.2 point 4, §4.3 point 4.
  • verdict: spec change present and matches the code. Details:
    • 0020 §4.2.4 is now limited to the regular write.
    • 0020 §4.3.4 states that the sudo copy is unbounded.
    • 0086 T3d is added.
    • 0086 §1 is imprecise, see Non-blocking (spec review's blocking finding overruled with the reason given there).
  • boundary, CI changes, vendor: ok. No .github/ changes, and the scope stays within the issue on the release branch. The changelog fragment is fixed ("upload comparison preview"). The threat-model note "no change" holds.

Security checks

  • Checked:
    • The regular write fails closed before the target is written: a backup error returns before write_file(path, ..).
    • Non-permission errors do not reach the sudo fallback.
    • Cleanup removes a symlink at the backup name, never its target.
    • No unredacted content in errors or logs.
    • Nothing was loosened against 3d4b57ed or the base, apart from the narrow possible case below.
  • security review (subagent): 7 bypass attempts (ENOSPC on the first chunk, EACCES on an existing backup name, planted symlink, directory at the backup name, empty file plus write failure, oversized source, permission denied leading to the sudo fallback) →
    • first-chunk partial backup: fixed (confirmed)
    • remote_files.rs:401-405: a foreign, non-writable file that already sits at exactly <path>.smartssh-backup-<same second> in a writable, non-sticky directory makes create fail, but created is already true, so the cleanup deletes that file. The base left it alone. This needs a planted file or a same-second collision → non-blocking, unsure → Follow-ups
    • pre-existing, not new: a symlink planted at the predictable backup name makes create() follow it and overwrite its target. The base behaved the same → non-blocking, unsure → Follow-ups
    • earlier notes (unbounded sudo cp -p, the guard test matching only .read_file() are unchanged → Follow-ups

UI review

  • not a UI change

Previous findings

  • Spec 0020 overclaims for the sudo path → fixed in f301cc02 (§4.2.4 limited to the regular write, §4.3.4 half-sentence added).
  • Partial backup after a first-chunk or empty-file write failure → fixed in f301cc02 (new test with counter-proof).
  • Sudo "before" hash comment at remote_files.rs:527-528 → fixed in f301cc02.
  • Spec 0086 §1 and T3c twin → T3d added (fixed). §1 is rewritten but still imprecise → accepted as non-blocking (see above).
  • Changelog "Text preview" → fixed.

Follow-ups

  • Backup creation: open the backup with CREATE|EXCLUDE (already used in ssh-transport/src/sftp.rs:488) and mark it for cleanup only after that open succeeds. This closes both the deletion of a pre-existing foreign file at the backup name and writing through a planted symlink at the predictable name. (no existing issue)
  • The sudo fallback copies the old file with an unbounded sudo -S cp -p; a root-only target that never ends can fill the server disk. Abort before cp when the hash stopped because the file exceeded its stat size, or use a bounded copy. (no existing issue)
  • The backup copy via read_range reopens the file for each chunk; an atomic replace during the copy yields a mixed backup. Read from one open handle, or re-check size and mtime afterwards. (no existing issue)
  • Spec 0086 §1: wording as suggested under Non-blocking.
  • Tests: a failing empty-file backup write; an oversize case with a reported size of 256 KiB (partial copy, then probe, then removal).
  • write_via_sftp_with_backup / write_via_sudo_fallback: pass old_size: Option<u64> instead of existed plus old_size, and rename created to backup_may_exist.
  • The guard test no_unbounded_read_file.rs could also match ::read_file(.
  • Writing to pseudo files with a stat size of 0 through the AI write action now fails at the backup step. Consider a clearer user-facing message.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

0 participants